﻿<#
	File      = wmix.psm1
	Author    = Kirk "Poshoholic" Munro
	Publisher = Poshoholic Studios
	Copyright = © 2012 Poshoholic Studios. All rights reserved.
#>

<#
TODO:
(* = completed)
*  1. Generate functions in auto-versioned WmiFx module.
*  2. Increment module version whenever functions are added, reload module.
*  3. Set Output type information for generated Get functions.
*  4. For each type processed, create a .types.ps1xml file entry that doesn't show the "internal" WMI properties.
*  5. For getters, identify key properties for filtering and create parameters to allow for wildcard matching (Name, Id, Description, Path, perhaps other properties); accept string arrays.
*  6. Merge names parameters with filter parameter for single filter to use server-side filtering. Consider replacing Filter with a hashtable (like QAD cmdlets), or with a PSObject that can be string or hashtable to allow for WMI filtering or PowerShell wildcard filtering.
*  7. Roll-up all types.ps1xml files into a single file for faster import/removal of module.
*  8. Add enumeration support so that meaningful values are seen in result object data.
   9. Fix localization so that the wmix module is localizable and so that help for wmifx is returned at least in English.
  10. Generate actionable functions from ForEach-Object, Remove-WmiObject, Invoke-WmiMethod, and Get-WmiObject; call Getter from one parameter set, use pipeline input from other parameter set.
  11. Set Input type and Output type information for generated actionable functions.
 11b. Determine which properties on which classes refer to disk space and convert the values to KB, MB, GB, TB and PB in formatted output (or use script properties). Maybe automatable, maybe manual task.
  12. Pull descriptions for actions from WMI.
  13. Add Protocol parameter to every command, which allows users to specify WMI or CIM depending on the version of PowerShell they are using; if CIM is specified on a v2 machine, then an error is raised and the command exits.
  14. By default, CIM is used on v3 machines and WMI on v2.
#>

#region Enable strict mode.

Set-StrictMode -Version Latest

#endregion

#region Set the module export mode to explicit.

Export-ModuleMember

#endregion

#region Load the string table.

Import-LocalizedData -FileName StringTable -BindingVariable StringTable -UICulture en-US -ErrorAction SilentlyContinue
if ($host.CurrentUICulture.Name -ne 'en-US') {
	Import-LocalizedData -FileName StringTable -BindingVariable StringTable -ErrorAction SilentlyContinue
}

#endregion

#region Load function files and export any functions we want visible publicly.

. $PSScriptRoot\ConvertTo-Type.ps1
. $PSScriptRoot\ConvertTo-WmiFilter.ps1
. $PSScriptRoot\Get-WmiClass.ps1
. $PSScriptRoot\Use-PspxModule.ps1
. $PSScriptRoot\Update-WmiFunctionLibrary.ps1

Export-ModuleMember -Function Get-WmiClass,Update-WmiFunctionLibrary

New-Alias -Name Update-Wmifx -Value Update-WmiFunctionLibrary -ErrorAction SilentlyContinue
if ($?) {
	Export-ModuleMember -Alias Update-Wmifx
}

#endregion

#region Optionally auto-generate commands for the most commonly-used WMI classes if wmifx does not exist yet.

if (-not (Get-Module -ListAvailable -Name wmifx)) {
	$result = $host.UI.PromptForChoice(
		$StringTable.InitializeWmifxTitle,
		$StringTable.InitializeWmifxDescription,
		[System.Management.Automation.Host.ChoiceDescription[]]@($StringTable.PromptForChoiceYesLabel,$StringTable.PromptForChoiceNoLabel),
		0
	)
	if ($result -eq 0) {
		$namespacesToProcess = @('ROOT\cimv2')
		if (Get-WmiObject -Namespace ROOT -Class __NAMESPACE -Filter 'name="Virtualization"') {
			$namespacesToProcess += 'ROOT\Virtualization'
		}
		Update-WmiFunctionLibrary -Namespace $namespacesToProcess -Recurse
	}
}

#endregion